<!--配置测算列-->
<template>
  <el-dialog :title="title"
             :visible.sync="visible"
             :width="width"
             :close-on-click-modal='false'
             top="10vh"
             class="bonus fix-height v50h">
    <el-table
      :data="tableData"
      :height="'calc(100vh - 500px)'"
      v-loading="loading"
      ref="tableRef"
      @selection-change="handleSelectionChange"
      border>
      <el-table-column type="selection" width="55">

      </el-table-column>
      <el-table-column prop="" label="序号" width="55" align="center" header-align="center">
        <template slot-scope="scope">
          <span>{{scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="label"
        label="参与计算项"
        align="center"
       >
      </el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel(false)">取消</el-button>
      <el-button type="primary"  @click="handleSubmit" :loading="saveLoading">确定</el-button>
    </div>

  </el-dialog>
</template>

<script>
import {saveConfigItem} from '@/api/bonus/calculateuserprogramme'
import {getDic} from "@/api/bonus/common";
import {validatenull} from "@/util/validate";
export default {
  name: "configComputeItemForm",
  components:{
  },
  props:{

  },
  watch:{

  },
  data(){
    return{
      type:'add',
      title:'参与计算系数配置',
      width:'30%',
      visible:false,
      loading:false,
      saveLoading:false,
      tableData:[],
      multipleSelection:[],
      toggleRows:[]
    }
  },
  methods:{
    init(data){
      this.parentData = data ;
      // if (type == 'edit'){
      //
      // }else{
      // }
      //获取所有可以参与计算项的接口
      this.getItemsDic() ;
      this.visible = true ;
    },
    getItemsDic(){
      this.loading = true ;
      getDic('bonus_person_compute_base').then(res=>{
        this.tableData = res.data.data
        if ( !validatenull(this.parentData.viewColumn)){
           this.$nextTick(()=>{
             this.tableData.map(item=>{
               if (this.parentData.viewColumn.includes(item.value)){
                 this.$refs.tableRef.toggleRowSelection(item,true )
               }
             })
           })
        }
        this.loading = false;
      }).catch(err=>{
        this.loading = false
      })
    },
    handleSubmit(){
      let selectedRows = this.$refs.tableRef.selection || [];
      if (selectedRows.length == 0){
        this.$message.error('请选择一条数据')
        return ;
      }
      let rows = selectedRows.map(item=>{
        return item.value
      })
      this.saveLoading = true ;
      saveConfigItem({id:this.parentData.id,viewColumn:rows.toString()}).then(res=>{
        if(res.data.code==0){
          this.$message.success("配置成功!")
          this.handleCancel(true)
        }
        this.saveLoading = false ;
      }).catch(err => {
        this.saveLoading = false ;
      })
    },
    handleCancel(refresh){
      if (refresh){
        this.$emit('refresh')
      }
      this.visible = false ;
    },
    handleSelectionChange(list){
      this.multipleSelection = list;

    }
  }
}
</script>

<style scoped>

</style>
